﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SelfMadeMillionaire.DAL.Repositories
{
	public class SubscriberRepository : Repository<Subscriber>, ISubscriberRepository
	{
		public SubscriberRepository(IUnitOfWork uow)
			: base(uow)
		{
		}

		public override Subscriber Update(Subscriber entity)
		{
			var subscriber = Get(entity.ID) ?? Get(entity.Email);

			if (subscriber != null)
			{
				subscriber.FirstName = entity.FirstName;
				subscriber.LastName = entity.LastName;
				subscriber.Email = entity.Email;
				subscriber.ProductType = entity.ProductType;
				subscriber.Status = entity.Status;
				subscriber.SubscriptionLevel = entity.SubscriptionLevel ?? subscriber.SubscriptionLevel;
				subscriber.PasswordHash = string.IsNullOrWhiteSpace(entity.PasswordHash) ? 
											subscriber.PasswordHash : 
											entity.PasswordHash;
				subscriber.Token = entity.Token;
                subscriber.TesterAccount = entity.TesterAccount;
				Commit();
			}

			return subscriber;
		}


		public Subscriber Get(string email)
		{
			return _dbSet.SingleOrDefault(x => x.Email == email);
		}

		public Subscriber Get(Guid token)
		{
			return _dbSet.SingleOrDefault(x => x.Token == token);
		}

        public Subscriber GetByPasswordRecoveryKey(Guid key)
        {
            return _dbSet.SingleOrDefault(x => x.PasswordRecoveryKey == key);
        }

        public int CountByDateCreate(DateTime begDate, DateTime endDate, SubscriptionType? level)
        {
            endDate = endDate.AddDays(1);
            
            bool levelNull = !level.HasValue;
            byte levelByte = level.HasValue ? (byte)level : (byte)0;

            return _dbSet.Count(x => (
                                        (x.CreatedDate >= begDate) && 
                                        (x.CreatedDate < endDate) && 
                                        (
                                            ((x.SubscriptionLevel == levelByte) && !x.SmmUnsubscribed)
                                            || (levelNull & !x.MmmUnsubscribed)
                                        ) 
                                      )
                               );
        }

        public void UnsubscribeSP(string email)
        {
            DataContext ctx = new DataContext();
            ctx.Database.ExecuteSqlCommand("exec spUnsubscribe @email", new SqlParameter("@email", email));

        }

        #region Mailing
        public void UpdateMODetail_EmailOpen(string mailing)
        {
            DataContext ctx = new DataContext();
            ctx.Database.ExecuteSqlCommand("exec spUpdateMODetail_EmailOpen @mailing", new SqlParameter("@mailing", mailing));
        }

        public void UpdateMODetail_LandingOpen(string mailing)
        {
            DataContext ctx = new DataContext();
            ctx.Database.ExecuteSqlCommand("exec spUpdateMODetail_LandingOpen @mailing", new SqlParameter("@mailing", mailing));
        }

        public void UpdateMODetail_SubscribeRedirect(string mailing)
        {
            DataContext ctx = new DataContext();
            ctx.Database.ExecuteSqlCommand("exec spUpdateMODetail_SubscribeRedirect @mailing", new SqlParameter("@mailing", mailing));
        }

        public void UpdateMODetail_SubscribeFinish(string mailing)
        {
            DataContext ctx = new DataContext();
            ctx.Database.ExecuteSqlCommand("exec spUpdateMODetail_SubscribeFinish @mailing", new SqlParameter("@mailing", mailing));
        }
        #endregion
    }
}
